#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef _WIN32
#include <windows.h>
#include <windowsx.h>
#else
#include <sys/time.h>
#include <time.h>
#include "unistd.h"
#endif

#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "stdarg.h"
#include "pthread.h"

#include "symbol.h"

#include "AWEntity.h"

pthread_mutex_t debug_report_file_mutex=PTHREAD_MUTEX_INITIALIZER; 
bool AW_Entity::debug_report=false;
FILE *AW_Entity::debug_report_file=0;
char AW_Entity::debug_report_filename[256]="report.txt";


#ifndef _WIN32
struct timeval init_tick_count_value;

void setupTickCount()
{
	gettimeofday(&init_tick_count_value, NULL);
}

long GetTickCount()
{
	struct timeval now;
	gettimeofday(&now, NULL);
	struct timeval diff;
	diff.tv_sec = now.tv_sec - init_tick_count_value.tv_sec;
	diff.tv_usec = now.tv_usec - init_tick_count_value.tv_usec;
	if (diff.tv_usec < 0)
	{
		diff.tv_sec--;
		diff.tv_usec+=1000000;
	}
	return diff.tv_sec*1000 + diff.tv_usec/1000;
}
#endif


AW_Entity::AW_Entity(void)
{
	class_name=new Symbol("AW_Entity");
} /* AW_Entity::AW_Entity */ 


AW_Entity::AW_Entity(FILE *fp)
{
	load(fp);
} /* AW_Entity::AW_Entity */ 


AW_Entity::~AW_Entity(void)
{
	delete class_name;
	class_name=0;
} /* AW_Entity::~AW_Entity */ 


bool AW_Entity::load(FILE *fp)
{
	return true;
} /* AW_Entity::load */ 


bool AW_Entity::save(FILE *fp)
{
	class_name->save(fp);

	return true;
} /* AW_Entity::save */ 



bool AW_Entity::is_a(char *c)
{
	return (strcmp(c,"AW_Entity")==0);
} /* AW_Entity::is_a */ 


bool AW_Entity::is_a(Symbol *c)
{
	return c->cmp("AW_Entity");
} /* AW_Entity::is_a */ 


Symbol *AW_Entity::get_class(void)
{
	return new Symbol(class_name);
} /* AW_Entity */ 


void AW_Entity::set_debug_report_filename(char *name)
{
	pthread_mutex_lock(&debug_report_file_mutex);

	if (debug_report_file!=0) fclose(debug_report_file);
	debug_report_file=0;
	strcpy(debug_report_filename,name);
	if (debug_report) debug_report_file=fopen(debug_report_filename,"w+");
	
	pthread_mutex_unlock(&debug_report_file_mutex);
} /* AW_Entity::set_debug_report_filename */ 


char *AW_Entity::get_debug_report_filename(void)
{
	char *tmp;
	pthread_mutex_lock(&debug_report_file_mutex);
	tmp=new char[strlen(debug_report_filename)+1];
	strcpy(tmp,debug_report_filename);
	pthread_mutex_unlock(&debug_report_file_mutex);
	return tmp;
} /* AW_Entity::get_debug_report_filename */ 


void AW_Entity::set_debug_report(void) 
{
	pthread_mutex_lock(&debug_report_file_mutex);
	debug_report=true;
	if (debug_report_file!=0) fclose(debug_report_file);
	debug_report_file=fopen(debug_report_filename,"w+");
	pthread_mutex_unlock(&debug_report_file_mutex);
} /* AW_Entity::set_debug_report */ 


void AW_Entity::unset_debug_report(void) 
{
	pthread_mutex_lock(&debug_report_file_mutex);
	debug_report=false;
	if (debug_report_file!=0) fclose(debug_report_file);
	debug_report_file=0;
	pthread_mutex_unlock(&debug_report_file_mutex);
} /* AW_Entity::unset_debug_report */ 


void AW_Entity::write_debug_report(const char *fmt, ...)						
{
	pthread_mutex_lock(&debug_report_file_mutex);
	if (fmt!=0 && debug_report_p() && debug_report_file!=0) {
		va_list		ap;										// Pointer To List Of Arguments

		va_start(ap, fmt);
		vfprintf(debug_report_file, fmt, ap);
		va_end(ap);							
		fflush(debug_report_file);
	} /* if */ 
	pthread_mutex_unlock(&debug_report_file_mutex);
} /* AW_Entity::write_debug_report */ 


void AW_Entity::pause(void)
{
} /* AW_Entity::pause */ 


void AW_Entity::unpause(void)
{
} /* AW_Entity::unpause */ 

